import { createRouter, createWebHashHistory } from "vue-router";

import Mainlayout from "@/layouts/mainlayout.vue";
import Index from "@/pages/index.vue";
import Login from "@/pages/login.vue";
import NotFound from "@/pages/404.vue";
import CreateProject from "@/pages/project/create_project.vue";
import ProjectList from "@/pages/project/list.vue";
import Statistics from "@/pages/backend/statistics.vue";
import TaggingProject from "@/pages/project_audit/get_tagging_project.vue";
import PendingProject from "@/pages/project_audit/get_pending_project.vue";
import AuditProject from "@/pages/project_audit/get_audited_project.vue";
import BatchManager from "@/pages/project_audit/batch_manager.vue";
import ResultAudit from "@/pages/project_audit/result_audit.vue";


const routes = [
  {
    path: "/",
    name: "/",
    component: Mainlayout,
    children: [
      {
        path: "/",
        component: Index,
        meta: {
          title: "IMS",
        },
      },
    ],
  },
  {
    path: "/login",
    component: Login,
    meta: {
      title: "登录",
    },
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: NotFound,
  },
];

// 动态路由
const asyncRoutes = [
  {
    path: "/project/create_project",
    name: "/project/create_project",
    component: CreateProject,
    meta: {
      title: "创建项目",
    },
  },
  {
    path: "/project/list",
    name: "/project/list",
    component: ProjectList,
    meta: {
      title: "已提交项目",
    },
  },
  {
    path: "/getStatisticsInfo",
    name: "/getStatisticsInfo",
    component: Statistics,
    meta: {
      title: "数据统计",
    },
  },
  {
    path: "/project_audit/batch_manager",
    name: "/project_audit/batch_manager",
    component: BatchManager,
    meta: {
      title: "批次管理",
    },
  },
  {
    path: "/project_audit/get_tagging_project",
    name: "/project_audit/get_tagging_project",
    component: TaggingProject,
    meta: {
      title: "待打标签项目",
    },
  },
  {
    path: "/project_audit/get_pending_project",
    name: "/project_audit/get_pending_project",
    component: PendingProject,
    meta: {
      title: "待评分项目",
    },
  },
  {
    path: "/project_audit/get_audited_project",
    name: "/project_audit/get_audited_project",
    component: AuditProject,
    meta: {
      title: "已评分项目",
    },
  },
  {
    path: "/project_audit/result_audit",
    name: "/project_audit/result_audit",
    component: ResultAudit,
    meta: {
      title: "批次项目列表",
    },
  },
];

export const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 动态添加路由
export function addRoutes(menus) {
  // 是否有新路由
  let hasNewRoutes = false;
  const findAndAddRoutesByMenus = (arr) => {
    arr.forEach((e) => {
      let item = asyncRoutes.find((o) => o.path == e.path);
      if (item && !router.hasRoute(item.path)) {
        router.addRoute("/", item);
        hasNewRoutes = true;
      }
      if (e.child && e.child.length > 0) {
        findAndAddRoutesByMenus(e.child);
      }
    });
  };

  findAndAddRoutesByMenus(menus);

  // console.log(router.getRoutes())
  return hasNewRoutes;
}
